package zuoshen_video;
import java.util.*;
public class HashProblems {
    //设计三个功能：主要，等概率随机返回任何一个可以
    class RandomPool<K> {
      
        private Map<K, Integer> main;
        private Map<Integer, K> help;
        private int count = 0;
        
        public RandomPool() {
           this.main = new HashMap<>();
           this.help = new HashMap<>();
        }
       /*  public int tableAt(Integer key) {
            int hash = key.hashCode();
            return hash ^ (capacity - 1);
        } */
         public void insert(K key) {
            main.put(key, count++);
            help.put(count, key);
        }

        public void delete(K key) {
            int c = main.remove(key);
            help.remove(c);
        }

        public K getRandom() {
            int size = main.size();
            return help.get((int)Math.random() * size);
        }
    }

    class BloomerFilter<T> {
        private  int CAPACITY = 1000000;
        private long[] bloom = new long[CAPACITY];
        public void setOne(T key) {
            //现实情况是多个哈希函数，这里只采用一个
            int hash = key.hashCode();

            //1.计算要沾的桶位
            int idx = hash / (CAPACITY - 1);
            //2.计算在这个桶位上的位置
            int i = hash % (CAPACITY - 1);
            //3.将这个位置设置为1
            bloom[idx] = bloom[idx] | (1 << i);
        }
    }
}
